package com.qzdsoft.erpcloud.controller.pu;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.com.CustomerSupplierContacts;
import com.qzdsoft.erpcloud.domain.sys.LoginUser;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.pu.PurchaseApplyService;
import com.qzdsoft.erpcloud.service.pu.PurchaseOrderService;
import com.qzdsoft.erpcloud.service.sale.SaleOrderService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.TablePermission;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierStatementInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderEditInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderInfo;
import com.qzdsoft.erpcloud.vo.pu.PurchaseOrderQueryInfo;
import com.qzdsoft.utils.ExceptionUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.TypeInfo;
import com.qzdsoft.vo.pu.PurchaseOrderSourceType;
import com.qzdsoft.vo.pu.PurchaseOrderStatus;

/**
 * 
 * 采购订单controller
 *
 * <p>detailed comment
 * @author pc-20170420 2017年9月5日
 * @see
 * @since 1.0
 */
@Controller
@RequestMapping("/pu/purchaseorder")
public class PurchaseOrderController extends BaseController
{

    private static final Logger logger = LoggerFactory.getLogger(PurchaseOrderController.class);
    private static final String BASEPATH = "pu/purchaseorder";

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    @Autowired
    private CustomerSupplierService customerSupplierService;
    
    @Autowired
    private PurchaseApplyService purchaseApplyService;
    
    @Autowired
    private SaleOrderService saleOrderService;
    
    @Autowired
    private CustomerSupplierStatementService customerSupplierStatementService;

    /**
     * 采购订单首页
     * @return
     */
    @TablePermission(id="3")
    @Permission(validate=false)
    @RequestMapping("/index")
    public ModelAndView index()
    {
        LoginUser user = getUser();
        ModelAndView mv = new ModelAndView();
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status", status);
        mv.addObject("companys",user.getCompanys());
        mv.setViewName(BASEPATH + "/index");
        return mv;
    }

    /**
     * 采购订单表格列表查询
     * @param info 查询条件封装
     * @return
     */
  
    //@SysLog(description="采购订单表格列表查询")
    @ResponseBody
    @RequestMapping("/list")
    public LayTableResponse<PurchaseOrderInfo> list(PurchaseOrderQueryInfo info)
    {
        logger.debug("查询采购订单，参数：{}",info);
        //如果初始化查询返回无数据空页
        if(info.isFirst()) {
            return LayTableResponse.nullDataPage();
        }else {
            return purchaseOrderService.findPurchaseOrdersBy(info);
        }
    }

    /**
     * 采购订单编辑
     * @param info
     * @return
     */
    @SysLog(description="采购订单编辑")
    @ResponseBody
    @RequestMapping("/edit")
    public Response<String> edit(PurchaseOrderEditInfo info)
    {
        return purchaseOrderService.edit(info);
    }
    
    /**
     * 采购订单新增页面
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/addinit")
    public ModelAndView addinit(Integer companyid) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("companyid",companyid);
        mv.addObject("types",PurchaseOrderSourceType.ALL);
        mv.setViewName(BASEPATH + "/add");
        return mv;
    }
    
    /**
     * 供应商结算方式查询
     * @param supplierid
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/querySettleMethod/{supplierid}")
    public List<CustomerSupplierStatementInfo> querySettleMethod(@PathVariable("supplierid")Integer supplierid) {
        //供应商结算方式
       LoginUser user = getUser();
       Integer companyid = user.getCompanyId();
       List<CustomerSupplierStatementInfo> customerSupplierStatements = customerSupplierStatementService.selectBy(companyid, supplierid);
       return customerSupplierStatements;
    }
    
    /**
     * 供应商税率信息查询
     * @param supplierid
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/queryTax/{supplierid}/{companyid}")
    public TypeInfo queryTax(@PathVariable("supplierid")Integer supplierid,@PathVariable("companyid")Integer companyid) {
        return customerSupplierService.selectTaxInfo(companyid, supplierid);
        
    }
    /**
     * 采购订单新增提交
     * @param 采购订单新增信息
     * @return
     */
    @SysLog(description="新增采购订单")
    @ResponseBody
    @RequestMapping("/add")
    public Response<String> add(PurchaseOrderEditInfo info){
        logger.debug("新增采购订单:{}",info);
        try {
            info.setCreatorId(getUser().getId());
            return  purchaseOrderService.add(info);
        }catch(Exception ex) {
            logger.error("新增采购订单错误，异常信息：{}",ExceptionUtil.getStackTrace(ex));
        }
        return new Response<>(ResultEnum.ERROR);
    }
  
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/querySourceOrder")
    public List<TypeInfo> querySourceOrder(String type,Integer companyid){
        if(PurchaseOrderSourceType.PURCHASE_APPLY_CODE.equals(type)) {
          return  purchaseApplyService.queryBillNo(companyid);
        }else {
            return saleOrderService.queryBillNo(companyid);
        }
    }

    /**
     * 修改采购订单页面跳转
     * @param companyid 公司id
     * @param id 采购订单id
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/{companyid}/{id}")
    public ModelAndView detail(@PathVariable("companyid") String companyid, @PathVariable("id") Integer id)
    {
        logger.debug("查询采购订单详情：companyid={},id={}",companyid,id);
        ModelAndView mv = new ModelAndView();
        PurchaseOrderInfo order = purchaseOrderService.findPurchaseOrderBy(id);
        mv.addObject("order", order);
        List<TypeInfo> status = PurchaseOrderStatus.ALL;
        mv.addObject("status", status);
        List<CustomerSupplierContacts> contacts = customerSupplierService.selectAll(order.getSupplierid()+"");
        mv.addObject("contacts",contacts);
        mv.addObject("types",PurchaseOrderSourceType.ALL);
        mv.setViewName(BASEPATH + "/edit");
        return mv;
    }
    
    /**
     * 审核采购订单
     * @param id 采购订单id
     * @return
     */
    @SysLog(description="审核采购订单")
    @ResponseBody
    @RequestMapping("/review")
    public Response<PurchaseOrderInfo> review(HttpServletRequest request,Integer id){
       Integer uid = getUid();
       return purchaseOrderService.reivew(uid,id);
    }
    
    /**
     * 弃审
     * @param id
     * @return
     */
    @SysLog(description="弃审采购订单")
    @ResponseBody
    @RequestMapping("/cancelreview")
    public Response<PurchaseOrderInfo> cancelReview(HttpServletRequest request,Integer id){
        Integer uid = getUid();
        return purchaseOrderService.cancelReview(uid,id);
    }
    
    /**
     * 关闭采购订单
     * @param id
     * @return
     */
    @SysLog(description="关闭采购订单")
    @ResponseBody
    @RequestMapping("/close")
    public Response<String> close(Integer id){
        return purchaseOrderService.close(id);
    }
    
    /**
     * 删除采购订单
     * @param id
     * @return
     */
    @SysLog(description="删除采购订单")
    @ResponseBody
    @RequestMapping("/remove")
    public Response<String> remove(Integer id){
        return purchaseOrderService.remove(id);
    }
}
